package com.wandian.ui.translation
{
	import com.wandian.net.SWFAssetLoader;
	import com.wandian.net.TranslationSWFLoader;
	
	import flash.events.*;
	import flash.net.*;
	import flash.system.*;
	import flash.xml.*;

	public class TranslationController extends EventDispatcher
	{
		private var fTranslationAssetsSWFUrl_str:String;
		private var fTranslationSWFLoader_tsl:TranslationSWFLoader;
		private var fExternTranslationModelWildReelUrl:String="";
		private var fAssetsDescriptions_tad_obj:Object;
		private var _languageId:String="english";
		private var gameDisp:GameDispatcher;
		private var fExternTranslationModelSwordFightUrl:String="";
		private var fExternTranslationModelClickMeUrl:String="";
		private var fTranslationAssetsReadyToUse_bl:Boolean;
		private var fExternTranslationModel_obj:Object;
		private var fTranslationXMLFilePath_str:String;
		public static const LANGUAGE_ID_ITALIAN:String="italian";
		public static const EVENT_LANGUAGE_XML_LOADED:String="onLanguageXMLLoaded";
		public static const EVENT_TRANSLATION_UPDATED:String="onTranslationAssetsReadyToUse";
		private static var fInstance_tc:TranslationController;
		public static const EVENT_ERROR_OCCURED:String="onErrorOccured";
		public static const LANGUAGE_ID_NORWEGIAN:String="norwegian";
		public static const LANGUAGE_ID_ENGLISH:String="english";
		public static const LANGUAGE_ID_GERMANY:String="germany";
		public static const LANGUAGE_ID_SWEDDISH:String="sweddish";
		public static const LANGUAGE_ID_RUSSIAN:String="russian";
		public static const LANGUAGE_ID_FINNISH:String="finnish";

		public function TranslationController(param1:GameDispatcher=null)
		{
			_languageId=LANGUAGE_ID_ENGLISH;
			fExternTranslationModelWildReelUrl="";
			fExternTranslationModelClickMeUrl="";
			fExternTranslationModelSwordFightUrl="";
			this.gameDisp=param1;
			if (TranslationController.fInstance_tc)
			{
				throw Error("Translation controller instance already exists.");
			}
			TranslationController.fInstance_tc=this;
			if (ParamsSpecification.getInstance().languageXMLUrl)
			{
				this.updateTranslationXMLUrlFromParamsSpecification();
			}
			else
			{
				ParamsSpecification.getInstance().addEventListener(ParamsSpecification.EVENT_LANGUAGE_XML_URL_PARAM_UPDATED, this.updateTranslationXMLUrlFromParamsSpecification);
			}
			return;
		} // end function

		protected function set translationAssetsSwfUrl(param1:String):void
		{
			this.fTranslationAssetsSWFUrl_str=param1;
			return;
		} // end function

		public function get translationSWFLoader():TranslationSWFLoader
		{
			if (!this.fTranslationSWFLoader_tsl)
			{
				this.fTranslationSWFLoader_tsl=new TranslationSWFLoader();
			}
			return this.fTranslationSWFLoader_tsl;
		} // end function

		protected function onTranslationAssetsLoadingCompleted(event:Event):void
		{
			this.onTranslationAssetsUpdated();
			return;
		} // end function

		public function get translationAssetsLibrary():ApplicationDomain
		{
			return this.translationSWFLoader.library;
		} // end function

		public function get languageId():String
		{
			return _languageId;
		} // end function

		public function set translationXMLFilePath(param1:String):void
		{
			this.fTranslationXMLFilePath_str=param1;
			this.updateTranslationAssets();
			return;
		} // end function

		public function get translationXMLFilePath():String
		{
			return this.fTranslationXMLFilePath_str;
		} // end function

		protected function onTranslationSWFLoadingProgress(event:Event):void
		{
			var _loc_2:int=0;
			_loc_2=Math.round(10 * this.translationSWFLoader.bytesLoaded / this.translationSWFLoader.bytesTotal);
			if (gameDisp.getLoading())
			{
				return;
			}
			if (_loc_2 > 0)
			{
				gameDisp.animatePreloader(40 + Math.ceil(_loc_2));
			}
			return;
		} // end function

		public function get assetsDescriptions():Object
		{
			return this.fAssetsDescriptions_tad_obj;
		} // end function

		protected function onTranslationSWFLoadingError(event:Event):void
		{
			trace("Error occured while trying to load translation SWF: " + this.translationAssetsSWFUrl);
			this.onErrorOccured();
			return;
		} // end function

		public function get wildReelTranslationAssetsSwfUrl():String
		{
			var _loc_1:String=null;
			_loc_1=this.fExternTranslationModelWildReelUrl;
			_loc_1=Const.getServerURL(gameDisp.loaderInfo.url) + _loc_1;
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_GV)
			{
				_loc_1=gameDisp.getUrlLoadedGv() + _loc_1;
			}
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_FINSOFT)
			{
				_loc_1=Const.getFlashURL(this.gameDisp.loaderInfo.url) + this.fExternTranslationModelWildReelUrl;
			}
			return _loc_1;
		} // end function

		protected function onTranslationAssetsUpdated():void
		{
			this.translationAssetsReady=true;
			this.dispatchEvent(new Event(EVENT_TRANSLATION_UPDATED));
			return;
		} // end function

		protected function loadTranslationXML():void
		{
			var _loc_1:URLLoader=null;
			var _loc_2:URLRequest=null;
			_loc_1=new URLLoader();
			_loc_1.addEventListener(Event.COMPLETE, this.onTranslationXMLLoadingCompleted);
			_loc_1.addEventListener(IOErrorEvent.IO_ERROR, this.onTranslationXMLLoadingError);
			fTranslationXMLFilePath_str=Const.getServerURL(gameDisp.loaderInfo.url) + fTranslationXMLFilePath_str;
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_GV)
			{
				fTranslationXMLFilePath_str=gameDisp.getUrlLoadedGv() + fTranslationXMLFilePath_str;
			}
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_FINSOFT)
			{
				fTranslationXMLFilePath_str=Const.getFlashURL(this.gameDisp.loaderInfo.url) + "translate/xml/language_" + gameDisp.getFinsoftLang() + ".xml";
			}
			_loc_2=new URLRequest(this.translationXMLFilePath);
			_loc_1.load(_loc_2);
			return;
		} // end function

		public function get swordFightTranslationAssetsSwfUrl():String
		{
			return this.fExternTranslationModelSwordFightUrl;
		} // end function

		protected function onTranslationXMLLoadingError(event:Event):void
		{
			trace("Error occured while trying to load translation XML: " + this.translationXMLFilePath);
			this.onErrorOccured();
			return;
		} // end function

		public function get externTranslationModel():Object
		{
			return this.fExternTranslationModel_obj;
		} // end function

		protected function parseTranslationXML(param1:String):void
		{
			var _loc_2:XMLDocument=null;
			var _loc_3:Array=null;
			var _loc_4:XMLNode=null;
			var _loc_5:TranslationAssetDescription=null;
			_loc_2=new XMLDocument();
			_loc_2.ignoreWhite=true;
			_loc_2.parseXML(param1);
			_loc_3=_loc_2.firstChild.childNodes;
			fAssetsDescriptions_tad_obj=new Object();
			for each (_loc_4 in _loc_3)
			{

				trace(_loc_4.attributes.id);
				if (_loc_4.attributes.id == "language_swf")
				{
					this.translationAssetsSwfUrl=_loc_4.attributes.url;
					continue;
				}
				if (_loc_4.attributes.id == "language")
				{
					this._languageId=_loc_4.attributes.text;
					continue;
				}
				if (_loc_4.attributes.id == "wildReelTranslationAssetsSwf")
				{
					fExternTranslationModelWildReelUrl=_loc_4.attributes.url;
				}
				if (_loc_4.attributes.id == "clickMeTranslationAssetsSwf")
				{
					fExternTranslationModelClickMeUrl=_loc_4.attributes.url;
				}
				if (_loc_4.attributes.id == "swordFightTranslationAssetsSwf")
				{
					this.fExternTranslationModelSwordFightUrl=_loc_4.attributes.url;
				}
				_loc_5=new TranslationAssetDescription();
				_loc_5.initFromXMLDescription(_loc_4.attributes);
				fAssetsDescriptions_tad_obj[_loc_5.assetId]=_loc_5;
			}
			this.loadTranslationAssets();
			this.gameDisp.updateFinsoftFunCurrency();
			return;
		} // end function

		public function get translationAssetsSWFUrl():String
		{
			var _loc_1:String=null;
			_loc_1=this.fTranslationAssetsSWFUrl_str;
			_loc_1=Const.getServerURL(gameDisp.loaderInfo.url) + _loc_1;
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_GV)
			{
				_loc_1=gameDisp.getUrlLoadedGv() + _loc_1;
			}
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_FINSOFT)
			{
				_loc_1=Const.getFlashURL(this.gameDisp.loaderInfo.url) + this.fTranslationAssetsSWFUrl_str;
			}
			return _loc_1;
		} // end function

		protected function updateTranslationAssets():void
		{
			this.loadTranslationXML();
			return;
		} // end function

		protected function loadTranslationAssets():void
		{
			this.dispatchEvent(new Event(EVENT_LANGUAGE_XML_LOADED));
			this.translationSWFLoader.addEventListener(SWFAssetLoader.EVENT_LOADING_COMPLETED, this.onTranslationAssetsLoadingCompleted);
			this.translationSWFLoader.addEventListener(SWFAssetLoader.EVENT_LOADING_ERROR_OCCURED, this.onTranslationSWFLoadingError);
			this.translationSWFLoader.addEventListener(SWFAssetLoader.EVENT_LOADING_PROGRESS_CHANGED, this.onTranslationSWFLoadingProgress);
			return;
		} // end function

		protected function updateTranslationXMLUrlFromParamsSpecification(event:Event=null):void
		{
			if (event)
			{
				ParamsSpecification(event.target).removeEventListener(ParamsSpecification.EVENT_LANGUAGE_XML_URL_PARAM_UPDATED, this.updateTranslationXMLUrlFromParamsSpecification);
			}
			this.translationXMLFilePath=ParamsSpecification.getInstance().languageXMLUrl;
			return;
		} // end function

		public function getAssetDescriptionById(param1:String):TranslationAssetDescription
		{
			var _loc_2:TranslationAssetDescription=null;
			_loc_2=this.assetsDescriptions[param1];
			if (!_loc_2)
			{
				throw Error("Asset description does not exist: " + param1);
			}
			return _loc_2;
		} // end function

		public function get translationAssetsReadyToUse():Boolean
		{
			return this.fTranslationAssetsReadyToUse_bl;
		} // end function

		protected function set translationAssetsReady(param1:Boolean):void
		{
			this.fTranslationAssetsReadyToUse_bl=param1;
			return;
		} // end function

		protected function onErrorOccured():void
		{
			this.dispatchEvent(new Event(EVENT_ERROR_OCCURED));
			return;
		} // end function

		public function set externTranslationModel(param1:Object):void
		{
			if (this.externTranslationModel)
			{
				throw Error("Extern translation model already defined!");
			}
			if (Object(this) == Object(param1))
			{
				return;
			}
			this.fExternTranslationModel_obj=param1;
			this.externTranslationModel.addEventListener(EVENT_TRANSLATION_UPDATED, this.dispatchEvent);
			return;
		} // end function

		public function get clickMeTranslationAssetsSwfUrl():String
		{
			var _loc_1:String=null;
			_loc_1=this.fExternTranslationModelClickMeUrl;
			_loc_1=Const.getServerURL(gameDisp.loaderInfo.url) + _loc_1;
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_GV)
			{
				_loc_1=gameDisp.getUrlLoadedGv() + _loc_1;
			}
			if (CustomerSpecification.getCustomerId() == CustomerSpecification.CUSTOMER_ID_FINSOFT)
			{
				_loc_1=Const.getFlashURL(this.gameDisp.loaderInfo.url) + this.fExternTranslationModelClickMeUrl;
			}
			return _loc_1;
		} // end function

		protected function onTranslationXMLLoadingCompleted(event:Event):void
		{
			this.parseTranslationXML(URLLoader(event.target).data);
			return;
		} // end function

		public static function getInstance():TranslationController
		{
			if (!TranslationController.fInstance_tc)
			{
				TranslationController.fInstance_tc=new TranslationController;
			}
			return TranslationController.fInstance_tc;
		} // end function

	}
}
